//[item_text, link, tip, Deshabilitado, Padre, icon_normal, icon_over] ;
/* funcioness que hacen posible esto*/
var menuItemsDefault = [
	["|Archivo","", ""       , "Login", "1", "Tip", ""],
		["||Iniciar Sesion","openSession('login');", "Login", "","Login","",""],
		["||Cerrar Sesion","openSession('logout');", "Cerrar Sesion", "1", "", "", ""],
	["|Ayuda",  "", "Help...", ""     , "1", ""   , ""],
			["||Acerca de.. ","openPopup('about.php');", "", "", "", "", "", "", "", "", "", ]
];


var _top_menu=1;
var _left_menu=1;
var _width_menu=120;
var _height_menu=20;
var _n_var="_div_m_";
var _n_menubar="menubar";
var _mm_menu = new Array();
var _dir_menu = "MENU";
var _m_n = "MENU/black.gif"; //Imagen de Fondo del menu
var _m_o = "MENU/blue.gif"; //Imagen de Fondo del menu cuando el mouse sta sobre
var _s_m_b = "#ffffff"; //color de fondo del sub menu
//var _s_m_b = "#D8D8D8"; //color de fondo del sub menu
var _s_m_c = "black"; //color de letra del sub menu
var _s_m_b_o = "#1C1C1C"; //color de fondo del sub menu seleccionado
var _s_m_c_o = "white"; //color de letra del sub menu seleccionado
var _m_d = "gray";	//color de letra deshabilitado
var _s_m_i = "MENU/blank.gif"; //fondo en blanco para el submenu
var _f_v="MENU/arrow_w.gif";  //Flecha que indica q el menu tiene hijos
var _f_h="MENU/arrow_b.gif"; //Flecha que indica q el submenu tiene hijos
var _mm_menuDiv = "_mm_menuDiv";
var _a_div_show = Array();
var _a_div_change = Array();
var _time_out;
var menuItems;

menuItems = menuItemsDefault;
function chklvl(array){
	if (array==null)return -1;
	var txt = array[0];
	var lvl=-1;
	var i=0;
	var ntxt;
	while(i<txt.length){
		ntxt = txt.substr(i,1);
		if (ntxt=='|'){lvl++;}
		else{break;}
		i++;
	}
	return lvl;
}
function _load_menu(){
	_mm_menu = new Array();
	for (var i=0;i<menuItems.length;i++){
		var lvl = chklvl(menuItems[i]);
		if 	(lvl==-1)continue;
		var smenu = "_mm_menu"
		var len = "_mm_menu"
		for (var j=0;j<lvl;j++){
			len += "[("+smenu+".length)-1]";
			smenu += "[("+smenu+".length)-1].array";
		}
		var menuO = new menuObject(menuItems[i],lvl, i);
		smenu+=".push(menuO)";
		eval (smenu);
	}
	return 0;
}
function showMenu(menubar){
	if (menubar==null)menubar = _n_menubar;
	if (_mm_menu.length==0) _load_menu();
	var sdiv="<div id='"+_mm_menuDiv+"' style='height:15px;position:relative;z-index:5;color:white;font:normal 11px Tahoma; '>";
	for (var i=0;i<_mm_menu.length;i++){
		sdiv+=readDiv(_mm_menu[i], i, _top_menu, (_left_menu+(_width_menu*i)), _width_menu, _height_menu);
	}
	sdiv +="</div>"
	$$(menubar).innerHTML=sdiv;
}
function readDiv(menu, id, t, l, w, h){
	var item= menu.menu;
	var text = _removePipe(item[0]);
	var ssdiv="";
	var style="style='";
	style+=" top:"+t+"px;";
	style+=" left:"+l+"px;";
	style+=" width:"+w+"px;";
	style+=" cursor:pointer;";
	//if (text=='-')style+=" height:10px;";
	//if (text=='-')h=10;
	//alert (h);
	style+=" height:"+h+"px;";
	style+=" position:absolute;";
	style+=" z-index:1;";
	if (menu.lvl!=0){style+=" font-weight:bolder; display:none;color:"+_s_m_c+";border:1px solid; width:"+(w+=20)+"px;background-color:"+_s_m_b+";border-top:0px;opacity:0.80;";}
	//Deshabilitado
	if (item[3]=="1"){style+=" color:"+_m_d+";"};
	style+=" ' ";
	ssdiv+="<div id='"+_n_var+"."+id+"' "+style+" ";
	if (item[3]!="1"){
		ssdiv+="onmouseOver='_displayMenu(this,"+menu.lvl+")' onmouseOut='_displayMenuOut(this,"+menu.lvl+")'";
		//ssdiv+="onmouseOver='_displayMenu(this,"+menu.lvl+")' onclick='_displayMenuOut(this,"+menu.lvl+")'";
		if (item[1]!="")
			ssdiv+='onclick="'+item[1]+'"';
		//ssdiv+="onclick='alert(this.id)'";
	}	
	if (item[2]!='') ssdiv+=" title='"+item[4]+"'";
	ssdiv+=" >";
	ssdiv+="<img style='width:100%;height:100%;position:absolute;' src='";
	if (menu.lvl==0){ssdiv+=_m_n;}
	else			{ssdiv+=_s_m_i;}
	ssdiv+="'>";

	ssdiv+="<span style='position:relative;display:block;height:100%; left:3px;margin-top:3px;background: url(checkk.gif) no-repeat;padding: 0 0 0 20px;'>";
	//ssdiv+="<span style='position:relative;display:block;height:100%; left:3px;margin-top:3px;'>";
	//
	ssdiv+= text=='-'?"<hr style='padding-left:3px;margin-right:3px;'>":text;
	ssdiv+="</span>";
	if (item[4]=="1"){
		ssdiv+="<img style='position:absolute;left:"+(w-12)+"px;top:6px;' src='";
		if (menu.lvl==0){ssdiv+=_f_v;}
		else			{ssdiv+=_f_h;}
		ssdiv+="'></img>";
	}
	ssdiv+="</img></div>";
	//if (item[3]=="1"){return ssdiv;}
	if (menu.array.length>0){	
		var nh=h;
		t+=nh;
		for (var i=0;i<menu.array.length;i++){
			nh=h;
			//alert (menu.menu+"->"+menu.lvl);
			if (_removePipe(menu.array[i].menu[0])=="-"){
				nh=10;
			}
			if (menu.lvl>=1){
				if (i==0){l=l+w;t=t-h;}
			}
			ssdiv+=readDiv(menu.array[i],id+"."+i, t, l, w, nh);
			t+=nh;
		}
	}
	return ssdiv;
}

function _displayMenu(Object, lvl){
	id = Object.id;
	if (_getLvl(id)==0){
		_hidden_Divs();
		_a_div_show = new Array();
	}
	for (var i=0;i<_a_div_show.length;i++){
		var _lvl = _getLvl(_a_div_show[i]);
		if (_lvl>lvl){
			$$(_a_div_show[i]).style.display='none';
		}
		//var exp = new RegExp('^'+_a_div_show[i]+'(.[0-9])*');
		/*if (!exp.test(id)){
			//alert ("hidden->"+id);
			$$(id).style.display='none';	
		}*/
	}
	if (lvl==0)	{Object.childNodes[0].src=_m_o;}
	else 		{Object.style.background=_s_m_b_o;Object.style.color=_s_m_c_o;}
	var ids = id+"."+"[0-9]";
	var divs = getElementsBy("id",ids,Object.parentNode,'div');
	for (var i=0;i<divs.length;i++){
		divs[i].style.display="";
		_a_div_show.push(divs[i].id);
		//alert (divs[i].id);
	}
	var c=0;
	for (var i=_n_var.length+1;i<id.length;i++){
		if (id[i]=='.'){
			var ids = id.substring(0,i);
			if ($$(ids)){
				if (c==0)	{($$(ids)).childNodes[0].src=_m_o;}
				else 		{($$(ids)).style.background=_s_m_b_o;($$(ids)).style.color=_s_m_c_o;}
				c++;
				_a_div_change.push(ids);
			}
		}
	}
	clearTimeout(_time_out);
}
function _displayMenuOut(Object, lvl){
	for (var i=0;i<_a_div_change.length;i++){
		if (i==0)	{$$(_a_div_change[i]).childNodes[0].src=_m_n;}
		else 		{$$(_a_div_change[i]).style.background=_s_m_b;$$(_a_div_change[i]).style.color=_s_m_c;}
	}
	_a_div_change = new Array();
	if (lvl==0)	{Object.childNodes[0].src=_m_n;}
	else 		{Object.style.background=_s_m_b;Object.style.color=_s_m_c;}
	var ids = id+"."+"[0-9]";
	var divs = getElementsBy("id",ids,Object.parentNode,'div');
	//for (var i=0;i<divs.length;i++){
	//if (lvl!=0)	Object.style.display="none";
	//}
	_time_out = setTimeout('_hidden_Divs()',500);
}
function _getLvl(id){
	var ids = id.substr(_n_var.length+1);
	return (ids.split('.').length)-1;
}
function _hidden_Div(id, o){
	/*if (o==null)var o = 1.0;
	o-=0.25;
	$$(id).style.opacity = o;
	if (o>0.1){
		setTimeout('_hidden_Div(\''+id+'\','+o+')',300);
	}
	else{
		$$(id).style.display = 'none';
		$$(id).style.opacity = 1;
	}*/
	$$(id).style.display='none';
}
function _hidden_Divs(){
	var div = $$('_mm_menuDiv');
	var divs = getElementsBy('id',"*",div,'div');
	for (var i=divs.length-1;i>0;i--){
		if ((_getLvl(divs[i].id))>0){
			_hidden_Div(divs[i].id);
		}
	}
}
function _removePipe(item){	
	var nitem;
	for (var i=0;i<item.length;i++) {
		nitem = item.substr(i,1);
		if (nitem!="|") break;
	}
	return item.substr(i);
}
function menuObject(menu, lvl, id){
	this.menu = menu;
	this.lvl = lvl;
	this.id = id;
	this.array = new Array();
	this.getArray = function(){return this.array;}
}
